package pre.liuxl.blog.repository;

import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.criterion.Criterion;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.orm.hibernate5.HibernateTemplate;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;
import pre.liuxl.blog.entity.User;
import pre.liuxl.blog.entity.UserArticle;
import pre.liuxl.blog.utils.HibernateSessionFactory;

import java.util.List;

/**
 * Created by liuxuanlong on 2018/6/24.
 */
@Repository
public class UserArticleRepository {
    @Autowired
    HibernateTemplate hibernateTemplate;

    @Transactional
    public void saveUserArticle(UserArticle userArticle) {

        hibernateTemplate.save(userArticle);
    }

    public List<UserArticle> findFirstTenOrderByCreateDate() {

        DetachedCriteria detachedCriteria = DetachedCriteria.forClass(UserArticle.class);
        detachedCriteria.addOrder(Order.desc("createDate"));
        return (List<UserArticle>) hibernateTemplate.findByCriteria(detachedCriteria, 0, 10);

    }

    public List<UserArticle> findFirstFiveOrderByCreateDate() {

        DetachedCriteria detachedCriteria = DetachedCriteria.forClass(UserArticle.class);
        detachedCriteria.addOrder(Order.desc("createDate"));
        return (List<UserArticle>) hibernateTemplate.findByCriteria(detachedCriteria, 0, 5);

    }

    public UserArticle findById(Integer id) {
        return hibernateTemplate.get(UserArticle.class, id);
    }

    public List<UserArticle> findFirstFiveOrderByPraiseNum() {
        DetachedCriteria detachedCriteria = DetachedCriteria.forClass(UserArticle.class);
        detachedCriteria.addOrder(Order.desc("praiseNum"));
        return (List<UserArticle>) hibernateTemplate.findByCriteria(detachedCriteria, 0, 5);
    }
}
